package com.service;

import com.aes.AES;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.StaffHistoryDAO;
import com.pojo.Dictionary;
import com.pojo.StaffHistory;
import com.util.StringUtil;
import com.util.ValidateHelper;
import com.vo.StaffHistoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

@Service
public class StaffHistoryService extends ServiceImpl<StaffHistoryDAO, StaffHistory> {

    @Autowired
    private StaffHistoryDAO staffHistoryDAO;

    @Autowired
    private DictionaryService dictionaryService;

    public static List<StaffHistory> histories = new Vector<>();

    /**
     * 根据身份证查员工
     * @param sfz  要查的身份证，支持查糊查询
     * @return 编号集合
     */
    public List<String> searchSfzNoZy(String sfz){
        List  ids = new ArrayList();
        if (StringUtil.isNull(sfz))
            return  ids;
        for (StaffHistory s: histories){
            String sfzStr = s.getSfz();

            if (sfzStr!=null && sfzStr.contains(sfz)){
                String id = s.getId();
                ids.add(id);
            }
        }
        return ids;
    }

    /**
     * 查出所有回收站人员证件信息
     * @return
     */
    public List<StaffHistory> getAllStaffWithSfz(){
        List<StaffHistory>  staffListTemp = new ArrayList();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("id","sfz","zhiye");
        List<StaffHistory> list = staffHistoryDAO.selectList(queryWrapper);
        for (StaffHistory staff:list){
            String sfz = staff.getSfz();
            String newSfz = staff.getSfz();
            if (sfz!=null && sfz.startsWith("$#")){
                newSfz = AES.decrypt(sfz.substring(3));
            }
            StaffHistory newStaff = new StaffHistory();
            newStaff.setId(staff.getId());
            newStaff.setSfz(newSfz);
            newStaff.setZhiye(staff.getZhiye());
            staffListTemp.add(newStaff);
        }
        histories.clear();
        histories = staffListTemp;
        return histories;
    }

    public IPage<StaffHistoryVo> selectStaffHis(String name, int page, int limit , String wuye , String qiye, String queryStr){
        Page p = new Page(page , limit) ;
        QueryWrapper<StaffHistory> query = new QueryWrapper() ;
        final String name1 = name ;
        if(StringUtil.isNotNull(name)) {
            query.and(q -> {
                q.like("name" , "%"+name1+"%").or().like("bh","%"+name1+"%").or().like("phone","%"+name1+"%").or().like("sfz","%"+name1+"%") ;
            }) ;
        }
        if(StringUtil.isNotNull(qiye)) {
            query.eq("qyId" , qiye) ;
        }
        //审核状态为已审核
        query.eq("shstatus",1);

        List<String> zyNameList = new ArrayList<>();
        if (ValidateHelper.isNotEmptyString(queryStr)) {
            List<Dictionary> zyList = new ArrayList<>();
            if ("gs".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyGs();
            }else if ("jt".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyJt();
            }
            if (ValidateHelper.isNotEmptyCollection(zyList)) {
                for (Dictionary dictionary : zyList) {
                    if (ValidateHelper.isNotEmptyString(dictionary.getName())) {
                        zyNameList.add(dictionary.getName());
                    }
                }
            }
            query.in("zhiye", zyNameList);
        }

        if(StringUtil.isNull(wuye)) {
            wuye = null ;
        }

        if(StringUtil.isNull(qiye)) {
            qiye = null ;
        }
        if(StringUtil.isNull(name)) {
            name = null ;
        }
        Integer status = 0;
        IPage<StaffHistoryVo> iPage = staffHistoryDAO.selectStaffHisByQyWy(p , name , wuye , qiye,status, queryStr, zyNameList) ;
        return iPage ;
    }


    public List<StaffHistory> getStaffHisByBh2(String bh) {
        QueryWrapper<StaffHistory> query = new QueryWrapper();
        query.eq("bh2",bh);
        query.orderByDesc("createtime");
        List<StaffHistory> staffHisList = staffHistoryDAO.selectList(query);
        if (staffHisList.size() > 0) {
            return staffHisList;
        }
        return new ArrayList<>();
    }

    public List<StaffHistory> getStaffHisByBh(String bh) {
        QueryWrapper<StaffHistory> query = new QueryWrapper();
        query.eq("bh",bh);
        query.orderByDesc("createtime");
        List<StaffHistory> staffHistoryList = staffHistoryDAO.selectList(query);
        if (staffHistoryList.size() > 0) {
            return staffHistoryList;
        }
        return new ArrayList<>();
    }

    /**
     * 根据证件号码查询员工历史
     * @param sfz
     * @return
     */
    public StaffHistory getStaffHistoryBySfz (String sfz) {
        QueryWrapper<StaffHistory> query = new QueryWrapper();
        query.eq("sfz", sfz);
        query.last("limit 1");
        StaffHistory staffHistory = staffHistoryDAO.selectOne(query);
        return staffHistory;
    }

    /**
     * 根据企业ID查询员工历史
     * @param qyId
     * @return
     */
    public List<StaffHistory> getStaffHistoryByQyId (String qyId) {
        QueryWrapper<StaffHistory> query = new QueryWrapper();
        query.eq("qyID", qyId);
        return staffHistoryDAO.selectList(query);
    }

}
